package com.gitee.wsl.mathematics.function.random


import com.gitee.wsl.ext.base.currentTime
import kotlin.jvm.JvmOverloads
import kotlin.time.ExperimentalTime

/**
 * A utility class that is used to generate pseudorandom numbers bases on XOR and bit shifts operations.
 * [Xorshift random generators](https://en.wikipedia.org/wiki/Xorshift) are simple and efficient software
 * implementations of number generators.
 */
@OptIn(ExperimentalTime::class)
class XorShiftRandom @JvmOverloads constructor(
    private var seed: Long = currentTime.nanosecondsOfSecond.toLong()
) {

    /**
     * Returns the next pseudorandom long value from this random number generator's sequence.
     *
     * @return the next pseudorandom long value
     */
    fun nextLong(): Long {
        seed = seed xor (seed ushr 12)
        seed = seed xor (seed shl 25)
        seed = seed xor (seed ushr 27)
        return seed * 0x2545F4914F6CDD1DL
    }
}